package com.silong.swing.myWindows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * Created by 龙哥 on 2022-01-17 15:38
 * Silong Software.
 */

public class ComboBoxFrame extends JFrame {

    JButton button1;
    JButton button2;

    JLabel lblInfo;
    JLabel lblHome;
    JComboBox<String> jcbGift;
    DefaultComboBoxModel<String> giftData;
    JComboBox<String> jcbProvince;
    JComboBox<String> jcbCity;
    DefaultComboBoxModel<String> provinceData;//省份
    DefaultComboBoxModel<String> cityData1;//省下市
    DefaultComboBoxModel<String> cityData2;//省下市
    DefaultComboBoxModel<String> cityData3;//省下市

    public ComboBoxFrame(){
        init();
    }

    private void init(){

        button1 = new JButton("确定");
        button2 = new JButton("退出");
        lblInfo = new JLabel("请选择一下礼物：");
        lblHome = new JLabel("请选择省份：");
        giftData = new DefaultComboBoxModel<String>();
        provinceData = new DefaultComboBoxModel<String>();
        cityData1 = new DefaultComboBoxModel<String>();
        cityData2 = new DefaultComboBoxModel<String>();
        cityData3 = new DefaultComboBoxModel<String>();
        jcbGift = new JComboBox<String>(giftData);
        jcbCity = new JComboBox<String>();
        jcbProvince = new JComboBox<String>();
        jcbGift.addItem("自行车"); //JComboBox的方法
        jcbGift.addItem("电动车");
        jcbGift.addItem("宝马车");
        jcbGift.setEditable(true); //组合框可以编辑状态 / false 是不可以编辑的
        provinceData.addElement("安徽"); //DefaultComboxModel方法
        provinceData.addElement("浙江");
        provinceData.addElement("江苏");
        cityData1.addElement("合肥");
        cityData1.addElement("安庆");
        cityData1.addElement("芜湖");
        cityData2.addElement("杭州");
        cityData2.addElement("宁波");
        cityData2.addElement("温州");
        cityData3.addElement("南京");
        cityData3.addElement("苏州");
        cityData3.addElement("昆山");
        jcbProvince.setModel(provinceData);;

        Container window = this.getContentPane();
        this.setSize(800, 600);
        this.setLocationRelativeTo(null); //距中
        this.setTitle("自定义控件大小位置");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出窗口关闭程序
        //设置布局为空，优点:摆放灵活，缺点：代码量大，自适应能力差。
        this.setLayout(null);

        lblInfo.setLocation(100,50); //设置按钮位置
        lblInfo.setSize(300,50); //设置按钮大小
        lblInfo.setFont(new Font("黑体", Font.BOLD, 20));

        jcbGift.setLocation(100,100); //设置按钮位置
        jcbGift.setSize(200,50); //设置按钮大小
        jcbGift.setFont(new Font("黑体", Font.BOLD, 20));

        lblHome.setLocation(100,180);
        lblHome.setSize(300,50);
        lblHome.setFont(new Font("黑体", Font.BOLD, 20));

        jcbProvince.setLocation(100,230);
        jcbProvince.setSize(250,50);
        jcbProvince.setFont(new Font("黑体", Font.BOLD, 20));

        jcbCity.setLocation(360,230);
        jcbCity.setSize(200,50);
        jcbCity.setFont(new Font("黑体", Font.BOLD, 20));

        //自定义按钮的位置 ，大小
        button1.setLocation(300,100); //设置按钮位置
        button1.setSize(80,50); //设置按钮大小
        button1.setFont(new Font("黑体", Font.BOLD, 20));
        //添加用户选择按钮监听
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("我选择了：" + jcbGift.getSelectedItem());
            }
        });

        button2.setFont(new Font("黑体", Font.BOLD, 20));
        button2.setBounds(400, 100, 80, 50); //一次性设置位置与大小
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        //添加省份选择代码
        jcbProvince.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                /*switch (jcbProvince.getItemAt(jcbProvince.getSelectedIndex())){
                    case "安徽":
                        jcbCity.setModel(cityData1);
                        break;
                    case "浙江":
                        jcbCity.setModel(cityData2);
                        break;
                    case "江苏":
                        jcbCity.setModel(cityData3);
                        break;
                }*/
                //选择时会发生两个事件，所以必须优化，所以只在发生选中时才触发
                if(e.getStateChange() == ItemEvent.SELECTED){
                    //仅选中时触发
                    System.out.println("------");
                    switch (jcbProvince.getSelectedItem().toString()){
                        case "安徽":
                            jcbCity.setModel(cityData1);
                            break;
                        case "浙江":
                            jcbCity.setModel(cityData2);
                            break;
                        case "江苏":
                            jcbCity.setModel(cityData3);
                            break;
                    }
                }
            }
        });
        window.add(button1);
        window.add(button2);
        window.add(lblInfo);
        window.add(jcbGift);
        window.add(lblHome);
        window.add(jcbProvince);
        window.add(jcbCity);

        //缺省显示第一个省份的值
        jcbCity.setModel(cityData1);
    }

    public static void main(String[] args) {
        ComboBoxFrame myFrame = new ComboBoxFrame();
        myFrame.setVisible(true);
    }
}
